<?php
class changepassword extends CI_Controller{
    protected $_templates;
	function __construct()
	{
        parent::__construct();
          $this->pre_message = "";        
          $this->_module_name = $this->router->fetch_module();
          $this->session->set_userdata(array('Url'=>uri_string()));
          $this->lang->load('access');
          $this->load->model('admin_model','admin');
      }
	  function index(){
          $this->acl->check('view','','',base_url());
		  $data=array();
		  $data['title'] = 'Change password';
            
            if(!empty($_POST['bt_submit']))
            {
                $admin_id = $this->session->userdata('AdminID'); 
                if($admin_id)
                {
                    $rs = $this->admin->getAdmin($admin_id);
                    if($rs&&($rs->Passwd!=md5($this->input->post('current_password')))){
                        $this->pre_message = lang('pw.current_password_not_match');
                    }
                    $this->form_validation->set_rules('current_password',lang('pw.current_password'),'trim|required');  
                    $this->form_validation->set_rules('new_password',lang('pw.new_password'),'trim|required');  
                    $this->form_validation->set_rules('retype_new_password',lang('pw.retype_password'),'trim|required|matches[new_password]'); 
                    if($this->form_validation->run() == FALSE)
                    {
                        $this->pre_message = validation_errors();
                    }
                    else
                    {
                        $new_password = md5($this->input->post('new_password'));
                        $rs = $this->admin->saveDataAdmin($admin_id,array('Passwd'=>$new_password));
                        if($rs){
                            $this->pre_message = lang('pw.change_password_successful');
                        }else{
                            $this->pre_message = lang('pw.change_password_unsuccessful');
                        }
                    }
                }else{
                    redirect('login/logout');
                }
            }
		  $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'admin/change_password_view';
          $this->site_lib->load($this->_templates['page'],$data);
	  }
  }
?>
